package edu.polsl.bio.str.common.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * //todo class description
 * <p/>
 * Created on: Jun 21, 2011 9:39:08 AM <br/>
 * <a href="http://www.polsl.pl">www.polsl.pl</a>
 *
 * @author lukasz.olczak@polsl.pl
 */
public class ProgressHelper {
  /**
   * slf4j Logger.
   */
  private static final Logger log = LoggerFactory.getLogger(ProgressHelper.class.getName());

  private static final int PRO_MIL_BASE = 1000;

  private static final int PER_CENT_BASE = 10;

  private int maxVal;

  private int perMilNotification;

  private int val;

  private int nextQuantum;

  public ProgressHelper(int maxVal, int perMilNotification) {
    this.maxVal = maxVal;
    this.perMilNotification = perMilNotification;
    val = 0;
    nextQuantum = perMilNotification;
  }

  public boolean collect() {
    val++;
    return isNotificationQuantum();
  }

  public boolean collect(int newVal) {
    val = newVal;
    return isNotificationQuantum();
  }

  public boolean isNotificationQuantum() {
    int quantum = getQuantum();
    if (quantum >= nextQuantum) {
      //todo fix it
      nextQuantum += perMilNotification;
      return true;
    }
    return false;
  }

  private int getQuantum() {
    return val * PRO_MIL_BASE / maxVal;
  }

  public int getProgressPercent() {
    return getQuantum() / PER_CENT_BASE;
  }
}
